// pages/user/user.js
var noLogin = require("../template/noLogin.js");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nickname: null,
    avatar: null,
    hasUserInfo: true    //判断是否获取了用户数据  默认true为获取了
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      noLogin.showNoLogin(this).then(() => {
        //用户授权后更新用户数据
        this.getUserInfos();
      }).catch(() => {
        this.setData({ hasUserInfo: false }); //判断用户未登录，弹出授权窗口
      });
    } else {
      this.setData({ hasUserInfo: true }); //判断用户已登录，关闭授权窗口
      this.getUserInfos();
    }
    // app.setBadge(2, wx.getStorageSync('car_num') || 0); //更新购物车数量
  },

  //获取用户信息
  getUserInfos(cb) {
    wx.showNavigationBarLoading();
    wx.showLoading({ title: '请稍等...', mask: true });
    const userId = app.globalData.userId || wx.getStorageSync('userId');
    // if (!userId) { app.tipLogin(); return; }
    wx.request({
      url: app.baseURL + 'Mine&a=index&user_id=' + userId,
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data.status == 3 || !res.data.member_data) {
            wx.showToast({
              title: "获取数据失败，请稍重试",
              icon: "none"
            })
          } else {
            this.setData({
              nickname: res.data.member_data.nickname,
              avatar: 'https://try.0791jr.com/data/attachment/avatar/' + res.data.member_data.avatar,
              wait_pay: res.data.wait_pay,
              wait_sent: res.data.wait_sent,
              wait_get: res.data.wait_get,
              wait_comment: res.data.wait_comment,
              is_distribute: res.data.is_distribute,   //判断分销商
              meId: res.data.member_data.id,
              act_list: res.data.act_list,  //拼团分销开关控制
            });
            //如果存在回调函数，执行
            cb && typeof cb == 'function' && cb();
          }
          wx.hideNavigationBarLoading();
          wx.hideLoading();
        }
      }
    })
  },

  //前往订单页面
  toOrder(e) {
    const jk = e.currentTarget.dataset.jk,
      type = e.currentTarget.dataset.type;
    console.log(jk,type)
    wx.navigateTo({
      url: '/pages/order/order?jk=' + jk + '&type=' + type
    })
  },

  //用户确认授权
  WeChatUserInfo: function (e) {
    if (e.detail.userInfo) {
      this.setData({
        hasUserInfo: true
      });
      //发起登录
        //用户授权后更新用户数据
        noLogin.getUserInfo(this).then(()=>{
          this.getUserInfos();
        });
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '拒绝微信授权将无法使用部分功能',
        success: res => {
          if (res.confirm) {
            wx.switchTab({url: '/pages/index/index'});
            this.setData({ hasUserInfo: true });
          }
        }
      })
    }
  },

  toDistribution(){
    const data = this.data.is_distribute;
    if (data.is_distribute == 1){
      wx.navigateTo({
        url: '../distribution/pages/distribution/distribution',
      });
    }else{
      wx.navigateTo({
        url: '/pages/distribution/pages/invitation/invitation?id=' + this.data.meId + "&no=1",
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    //刷新用户数据
    this.getUserInfos(wx.stopPullDownRefresh());
  },
})